import { handleAuthGetUserInfo, handleGetUserPhomeNumber } from '../../utils/wx'
import { isLogin, toLogin, getUserInfo, clearCacheAll } from '../../utils/auth'
import http from '../../utils/http'
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        show: false,
        refundGoods: null,
        userInfo: null,
        refundId: null,
        expressNum: null,
        beforeClose: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
        let { refundId } = options

        this.setData({
            refundId,
            beforeClose: (action) => new Promise((resolve) => {
                let { expressNum } = this.data
                if (action === 'confirm') {
                    if(expressNum) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                } else {
                    this.setData({ expressNum: null })
                    resolve(true)
                }
            })
        })
        
    },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        // 用户登录信息
        this.setData({ userInfo: getUserInfo() });

        this.getRefundGoods();
	},

    /**
     * 获取的推荐信息
     */
     async getRefundGoods() {
        let { refundId } = this.data
        let res = await http.get('/api/mall/omsorderreturnapply/getOrderReturnApplyInfo/'+refundId)
        
        if(res.status) {
            this.setData({ refundGoods: res.data })
        }
    },

    showInputExpressInfo() {
        this.setData({ show: true })
    },
    
    async onConfirm(e) {
        let { expressNum, refundGoods, userInfo } = this.data
        if(expressNum) {
            let res = await http.post('/api/mall/omsorderreturnapply/submitExpress', {id: refundGoods.id, memberId: userInfo.id, expressNum})
            if(res.status) {
                this.setData({ expressNum: null, show: false })
                this.getRefundGoods()
            }
        } else {
            showToast({title: '请输入快递单号'})
            return false
        }
    },
    onCancel() {
        this.setData({ showPwd: false, canSendSms: true })
    },

    onUnload: function () {
        wx.reLaunch({
          url: `/pages/user/user`
        })
    },

})